package cn.icanci.loopstack.bic.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.NodeStyleDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.NodeStyleDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/13 23:32
 */
@Service("nodeStyleDAO")
public class MongoNodeStyleDAO extends AbstractBaseDAO<NodeStyleDO> implements NodeStyleDAO {
    @Override
    public void insert(NodeStyleDO nodeStyleDO) {
        super.insert(nodeStyleDO);
        mongoTemplate.insert(nodeStyleDO, COLLECTION_NAME);
    }

    @Override
    public void update(NodeStyleDO nodeStyleDO) {
        super.update(nodeStyleDO);
        mongoTemplate.save(nodeStyleDO, COLLECTION_NAME);
    }

    @Override
    public List<NodeStyleDO> queryAll() {
        Criteria criteria = Criteria.where(NodeStyleColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);

    }

    @Override
    public PageList<NodeStyleDO> pageQuery(NodeStyleDO nodeStyleDO, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(NodeStyleColumn.ENV).is(DEFAULT_ENV);
        if (StringUtils.isNotBlank(nodeStyleDO.getNodeStyleName())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(NodeStyleColumn.NODE_STYLE_NAME).regex("^.*" + nodeStyleDO.getNodeStyleName() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(nodeStyleDO.getNodeStyleKey())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(NodeStyleColumn.NODE_STYLE_KEY).regex("^.*" + nodeStyleDO.getNodeStyleKey() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(nodeStyleDO.getSystemUuid())) {
            criteria.and(NodeStyleColumn.SYSTEM_UUID).is(nodeStyleDO.getSystemUuid());
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, NodeStyleColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    @Override
    public NodeStyleDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(NodeStyleColumn._ID).is(_id);
        criteria.and(NodeStyleColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);

    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return NodeStyleColumn.class;
    }

    @Override
    public NodeStyleDO queryBySystemUuidAndName(String systemUuid, String name) {
        Criteria criteria = Criteria.where(NodeStyleColumn.NODE_STYLE_NAME).is(name);
        criteria.and(NodeStyleColumn.ENV).is(DEFAULT_ENV);
        criteria.and(NodeStyleColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);

    }

    @Override
    public NodeStyleDO queryBySystemUuidAndKey(String systemUuid, String key) {
        Criteria criteria = Criteria.where(NodeStyleColumn.NODE_STYLE_KEY).is(key);
        criteria.and(NodeStyleColumn.ENV).is(DEFAULT_ENV);
        criteria.and(NodeStyleColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);

    }

    @Override
    public NodeStyleDO queryByUuid(String styleUuid) {
        Criteria criteria = Criteria.where(NodeStyleColumn.UUID).is(styleUuid);
        criteria.and(NodeStyleColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }
}
